'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument	
dim cr
dim userLayerName:userLayerName="zyq_chk"
Dim usrLyrObj
main

set doc = nothing
sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	'// TODO
	args =0
	args = cdbl(args)
	doc.currentunit=2 'mm
	Dim NColorObject
	set NColorObject = application.utility.NewColorPattern  
	With NColorObject
				.red = 255
				.green =255
				.blue =0
				.index =0
				.outline=false
				.Transparent=false
	end With
	call CheckUserlyr_withColor(userLayerName,NColorObject)
	
	if args= 0 then
		'msgbox args
		Set usrLyrObj = doc.FindUserLayer(usrLyrObj)
		If usrLyrObj Is Nothing Then
			Set usrLyrObj = doc.SetupParameter.PutUserLayer(userLayerName)
		Else
			doc.UserLayerGfxs(, userLayerName).Delete
		End If
	else
		call putTestPointDisArea()
	end if	
end sub

sub putTestPointDisArea()
	application.gui.processkeyin("zx")		
	dim minx,maxx,miny,maxy
	minx = doc.BoardOutline.Extrema.minx
	maxx = doc.BoardOutline.Extrema.maxx
	miny = doc.BoardOutline.Extrema.miny
	maxy = doc.BoardOutline.Extrema.maxy
	
	
	dim xc,yc
	xc =doc.ActiveView.CenterX 
	yc =doc.ActiveView.CenterY 
	
	dim usrLyrObj
	Set usrLyrObj = doc.FindUserLayer(userLayerName)
	
	
	Dim pntsArr()
	ReDim pntsArr(1,1)
	'XLINE
	pntsArr(0,0) = minx
	pntsArr(1,0) = yc
	pntsArr(0,1) = maxx
	pntsArr(1,1) = yc
	numPoints = UBound(pntsArr,2) +1
	set fabGfxObj = doc.PutUserLayerGfx(usrLyrObj,0.1,numPoints,pntsArr,False,Nothing,epcbUnitCurrent)
	'YLINE
	pntsArr(0,0) = xc
	pntsArr(1,0) = miny
	pntsArr(0,1) = xc
	pntsArr(1,1) = maxy
	numPoints = UBound(pntsArr,2) +1
	set fabGfxObj = doc.PutUserLayerGfx(usrLyrObj,0.1,numPoints,pntsArr,False,Nothing,epcbUnitCurrent)	
	'round
	doc.currentunit=4 'mm
	xc =doc.ActiveView.CenterX 
	yc =doc.ActiveView.CenterY 
	dim cr:cr = args'inputbox("dmm",cr,1)
	pntArr = application.Utility.CreateCircleXYR(xc,yc,cr)
	numPoints = UBound(pntArr, 2) + 1		
	set fabGfxObj = doc.PutUserLayerGfx(usrLyrObj,0.01,numPoints,pntArr,false,Nothing,4) 'mm
	
end sub
